#!/usr/bin/env python

import  time
import  random
from bisect import bisect_left, insort

class   TestBisect(object) :
    def setUp(self) :
        pass
        ## setUp()

    def tearDown(self) :
        pass
        # tearDown()

    def run(self) :
        self.testSimple(10)
        self.testSimple(20)
        self.testSimple(50)
        self.testSimple(100)
        self.testSimple(200)
        self.testSimple(500)
        self.testSimple(1000)

        self.testTuple2(10)
        self.testTuple2(20)
        self.testTuple2(50)
        self.testTuple2(100)
        self.testTuple2(200)
        self.testTuple2(500)
        self.testTuple2(1000)

        self.testTuple3(10)
        self.testTuple3(20)
        self.testTuple3(50)
        self.testTuple3(100)
        self.testTuple3(200)
        self.testTuple3(500)
        self.testTuple3(1000)

        self.testTuple4(10)
        self.testTuple4(20)
        self.testTuple4(50)
        self.testTuple4(100)
        self.testTuple4(200)
        self.testTuple4(500)
        self.testTuple4(1000)

        self.testStrSimple(10)
        self.testStrSimple(20)
        self.testStrSimple(50)
        self.testStrSimple(100)
        self.testStrSimple(200)
        self.testStrSimple(500)
        self.testStrSimple(1000)

        self.testStrTuple2(10)
        self.testStrTuple2(20)
        self.testStrTuple2(50)
        self.testStrTuple2(100)
        self.testStrTuple2(200)
        self.testStrTuple2(500)
        self.testStrTuple2(1000)

        self.testStrTuple3(10)
        self.testStrTuple3(20)
        self.testStrTuple3(50)
        self.testStrTuple3(100)
        self.testStrTuple3(200)
        self.testStrTuple3(500)
        self.testStrTuple3(1000)

        self.testStrTuple4(10)
        self.testStrTuple4(20)
        self.testStrTuple4(50)
        self.testStrTuple4(100)
        self.testStrTuple4(200)
        self.testStrTuple4(500)
        self.testStrTuple4(1000)
        # run()

    def testSimple(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, x)
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'SIMPLE : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, x)
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'SIMPLE : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testSimple()

    def testTuple2(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (x, x+11))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'TUPLE-2 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (x, 0))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'TUPLE-2 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testTuple2()

    def testTuple3(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (x, x+11, x+23))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'TUPLE-3 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (x, x+7, x+19))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'TUPLE-3 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testTuple3()

    def testTuple4(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (x, x+11, x+37, x+19))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'TUPLE-4 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (x, x+11, x+17, 0))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'TUPLE-4 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testTuple4()

    def testStrSimple(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, str(x))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'STR-SIMPLE : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, str(x))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'STR-SIMPLE : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testStrSimple()

    def testStrTuple2(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (str(x), str(x+11)))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'STR-TUPLE-2 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (str(x), None))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'STR-TUPLE-2 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testStrTuple2()

    def testStrTuple3(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (str(x), str(x+11), str(x+23)))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'STR-TUPLE-3 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (str(x), str(x+7), str(x+19)))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'STR-TUPLE-3 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testStrTuple3()

    def testStrTuple4(self, element_count) :
        a = []
        time_start = time.time()
        for i in xrange(element_count) :
            x = random.randint(1,10000)
            insort(a, (str(x), str(x+11), str(x+37), str(x+19)))
        time_last = time.time() - time_start
        tps = element_count / time_last
        print 'STR-TUPLE-4 : insort %d elements last %.3f seconds, tps=%.2f' % (element_count, time_last, tps)

        SEARCH_TIMES = 10000
        time_start = time.time()
        for i in xrange(SEARCH_TIMES) :
            x = random.randint(1,10000)
            bisect_left(a, (str(x), str(x+11), str(x+17), None))
        time_last = time.time() - time_start
        tps = SEARCH_TIMES / time_last
        print 'STR-TUPLE-4 : bisect %d times in %d-element-list last %.3f seconds, tps=%.2f' % (SEARCH_TIMES, element_count, time_last, tps)
        ## testStrTuple4()

    ## class TestBisect

if  __name__ == '__main__' :
    t = TestBisect()
    t.setUp()
    try :
        t.run()
    finally :
        t.tearDown()

